﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net;
using System.Text;
using System.IO;

namespace SocialCee.WebApi.Tests
{
    [TestClass]
    public class WebRequests
    {
        string rawCred = "marcin:marcin";

        [TestMethod]
        public void Get_Unauth()
        {
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            var rawCred = "marcin:marcin";
            var cred = Convert.ToBase64String(encoding.GetBytes(rawCred));

            SendRequest(cred);
        }


        [TestMethod]
        public void Get()
        {
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            string auth = string.Format("{0}:{1}", "test2", "akhsdgfal$%#@SHlg12");
            string enc = Convert.ToBase64String(Encoding.ASCII.GetBytes(auth));
            string rawCred = string.Format("{0} {1}", "Basic", enc);

            SendRequest(rawCred);
        }

        private static void SendRequest(string cred)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost/SocialCee.WebApi/api/values");

            //request.Method = "POST";
            //request.ContentType = "text/plain;charset=utf-8";
            //request.Headers.Add(HttpRequestHeader.Authorization, cred);

            request.Headers[HttpRequestHeader.Authorization] = cred;
            //request.ContentLength = 0;

            WebResponse response = request.GetResponse();
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                string result = reader.ReadToEnd();
                if (!string.IsNullOrEmpty(result))
                { }
            }
        }
    }
}
